package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanException;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nuringProjectMapper;

    /**
     * 分页查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> pageQuery(String name, Integer status, Integer pageNum, Integer pageSize) {
        //调用mapper
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page = nuringProjectMapper.pageQuery(name, status);
        //返回vo
       /* PageResponse<NursingProjectVo> pageResponse = new PageResponse();
        pageResponse.setPages((long) page.getPages());
        pageResponse.setTotal(page.getTotal());
        pageResponse.setPage(pageNum);
        pageResponse.setPageSize(pageSize);
        pageResponse.setRecords(BeanUtil.copyToList(page.getResult(), NursingProjectVo.class));
        return pageResponse;*/
        return PageResponse.of(page, NursingProjectVo.class);
    }

    /**
     * 新增护理项目
     *
     * @param dto
     */
    @Override
    public void addNursingProject(NursingProjectDto dto) {
        Integer count = nuringProjectMapper.selectNursingProByName(dto.getName());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSINGPROJECT_NAME_EXICT);
        }
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        nuringProjectMapper.insertNursingProject(nursingProject);
    }

    /**
     * 根据id查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getNursingProjectById(Long id) {
        NursingProject nursingProject = nuringProjectMapper.getNursingProjectById(id);
        NursingProjectVo vo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return vo;
    }

    /**
     * 编辑护理项目
     *
     * @param dto
     */
    @Override
    public void update(NursingProjectDto dto) {
        Integer count = nuringProjectMapper.selectNursingProByNameAndId(dto.getName(), dto.getId());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSINGPROJECT_NAME_EXICT);
        }
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        nuringProjectMapper.updateNursingProject(nursingProject);
    }

    /**
     * 删除护理项目
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //TODO 查询护理计划和项目关联表.看护理项目是否有关联的护理计划
/*     Integer count = nuringProjectMapper.selectCountNursingPlan(id);
     if (count >0){
         throw new BaseException(BasicEnum.NURSINGPROJECT_PLAN_EXICT);
     }*/

        //状态为启用状态不能删除
        NursingProject nursingProject = nuringProjectMapper.getNursingProjectById(id);
        if (nursingProject == null) {
            throw new BaseException(BasicEnum.NURSINGPROJECT_NOT_EXICT);
        }
        if (nursingProject.getStatus() == 1) {
            throw new BaseException(BasicEnum.NURSINGPROJECT_STATUS_OPEN);
        }
        nuringProjectMapper.deleteNursingProjectById(id);
    }

    /**
     * 启用禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nuringProjectMapper.updateStatus(id, status);
    }

    /**
     * 查询全部护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> getAllNursingProject() {
        List<NursingProject> list = nuringProjectMapper.getAllNursingProjects();
        List<NursingProjectVo> voList = BeanUtil.copyToList(list, NursingProjectVo.class);
        return voList;
    }
}
